﻿using System;
using System.Web.UI;
using Pantry.Business;
using Pantry.Commons;
using Pantry.Entity;
namespace Pantry.Web.Users.Controls
{
    public delegate void SendValuesListAddress(string valuesToThePage);
    public partial class ListAddress : System.Web.UI.UserControl
    {
        public event SendValuesListAddress SendvaluesToThePage;
        protected void Page_Load(object sender, EventArgs e)
        {
            FilterAddress(txtPostcode.Text, txtStreet.Text, txtTown.Text);
        }

        private void FilterAddress(string postcode, string street, string town)
        {
            try
            {
                if (postcode != null || street != null ||
                    town != null)
                {
                    var address = new AddressSearch
                    {
                        Postcode = postcode,
                        Address = street,
                        Town = town
                    };
                    CollectionPager.PageSize = 5;
                    CollectionPager.BindToControl = RepeaterAddress;
                    CollectionPager.DataSource =  new AddressBus().ReturnSearchValue(address);

                    if (CollectionPager.TotalRecords != 0)
                    {
                        RepeaterAddress.DataSource = CollectionPager.DataSourcePaged;
                        RepeaterAddress.DataBind();
                    }
                    else
                    {

                        var dt =  new AddressBus().ListAddressesToDataSet().Tables[0];
                        CollectionPager.PageSize = 5;
                        CollectionPager.BindToControl = RepeaterAddress;
                        CollectionPager.DataSource = dt.DefaultView;
                        RepeaterAddress.DataSource = CollectionPager.DataSourcePaged;
                        RepeaterAddress.DataBind();
                    }

                }
                else
                {
                    var dt =  new AddressBus().ListAddressesToDataSet().Tables[0];
                    CollectionPager.PageSize = 5;
                    CollectionPager.BindToControl = RepeaterAddress;
                    CollectionPager.DataSource = dt.DefaultView;
                    RepeaterAddress.DataSource = CollectionPager.DataSourcePaged;
                    RepeaterAddress.DataBind();
                }
            }

            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }
        protected void AddressSearch_Click(object sender, EventArgs e)
        {
            FilterAddress(txtPostcode.Text, txtStreet.Text, txtTown.Text);
        }
        protected void None_Click(object sender, EventArgs e)
        {
            FilterAddress(txtPostcode.Text, txtStreet.Text, txtTown.Text);
        }
        protected void Clear_Click(object sender, EventArgs e)
        {
            txtPostcode.Text = null;
            txtStreet.Text = null;
            txtTown.Text = null;
        }
        protected void ButtonSelectClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(HttpHelper.GetFormString("RadioAddressId")))
            {
                var address = HttpHelper.GetFormString("RadioAddressId");
                if (SendvaluesToThePage != null)
                {
                    SendvaluesToThePage(address);
                }
                ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "Key", "CloseAddressPopup()", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey", "alert('Please select one row !');", true);
            }
        }
    }
}